package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzMonthlyLeave;
import com.gpframework.module.wages.domain.GzPersonnelFoundation;
import com.gpframework.module.wages.domain.req.GzGenerateReq;
import com.gpframework.module.wages.domain.req.GzMonthlyLeaveAdd;
import com.gpframework.module.wages.domain.req.GzMonthlyLeaveReq;
import com.gpframework.module.wages.domain.req.GzMonthlyLeaveUpdate;
import com.gpframework.module.wages.domain.vo.GzMonthlyLeaveVo;
import com.gpframework.module.wages.service.IGzMonthlyLeaveService;
import com.gpframework.module.wages.service.IGzPersonnelFoundationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
/**
 * @ClassName GzMonthlyLeaveController
 * @Author gpframework
 * @Date 2024-01-18
 * @Version 1.0.0
 * @Description 月度休假Controller
 */
@Validated
@Api(tags = "月度休假")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/monthlyleave" )
public class GzMonthlyLeaveController extends BaseController {

    private final IGzMonthlyLeaveService iGzMonthlyLeaveService;


    private final IGzPersonnelFoundationService iGzPersonnelFoundationService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-18
  * @Param gzMonthlyLeave
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:monthlyleave:list')")
    @GetMapping("/list")
    public TableDataInfo<GzMonthlyLeaveVo> list(GzMonthlyLeaveReq gzMonthlyLeaveReq)
    {
        startPage();
        QueryWrapper<GzMonthlyLeave> lqw = new QueryWrapper<GzMonthlyLeave>();
        gzMonthlyLeaveReq.generatorQuery(lqw,true);
        List<GzMonthlyLeaveVo> list = iGzMonthlyLeaveService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-18
     * @Param gzMonthlyLeave
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:monthlyleave:add')" )
    @Log(title = "添加月度休假" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzMonthlyLeaveAdd gzMonthlyLeaveAdd) {
        GzMonthlyLeave gzMonthlyLeave = Convert.convert(new TypeReference<GzMonthlyLeave>() {}, gzMonthlyLeaveAdd);
        gzMonthlyLeave.setRestDays(gzMonthlyLeaveAdd.getMonthlyDays()-gzMonthlyLeaveAdd.getRequireDateNdancedays());
        return toAjax(iGzMonthlyLeaveService.save(gzMonthlyLeave) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-18
     * @Param gzMonthlyLeave
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('wages:monthlyleave:edit')" )
    @Log(title = "修改月度休假" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzMonthlyLeaveUpdate gzMonthlyLeaveUpdate) {
        GzMonthlyLeave gzMonthlyLeave = Convert.convert(new TypeReference<GzMonthlyLeave>() {}, gzMonthlyLeaveUpdate);
        GzPersonnelFoundation personnelFoundation = iGzPersonnelFoundationService.getById(gzMonthlyLeaveUpdate.getUserId());
        personnelFoundation.setRequireDateNdancedays(gzMonthlyLeaveUpdate.getRequireDateNdancedays());
        iGzPersonnelFoundationService.updateById(personnelFoundation);
        return toAjax(iGzMonthlyLeaveService.updateById(gzMonthlyLeave) ? 1 : 0);
    }
    @ApiOperation("生成员工休假")
    @PreAuthorize("@ss.hasPermi('wages:monthlyleave:generateEmployeeLeave')" )
    @Log(title = "生成员工休假" , businessType = BusinessType.INSERT)
    @PostMapping("/generateEmployeeLeave")
    public AjaxResult generateEmployeeLeave(@RequestBody GzGenerateReq gzGenerateReq) throws ParseException {

        // 创建Calendar对象
        Calendar calendar = Calendar.getInstance();
        int month = gzGenerateReq.getYearMonth().getMonth();
        calendar.set(Calendar.MONTH, month);
        // 获取当前月份的天数
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);


        iGzMonthlyLeaveService.remove(new QueryWrapper<GzMonthlyLeave>()
                .eq("month",gzGenerateReq.getYearMonth()));
        iGzMonthlyLeaveService.delelteMonth(gzGenerateReq.getYearMonth());

        List<GzMonthlyLeave> gzMonthlyLeaves=new ArrayList<>();
        ArrayList<GzPersonnelFoundation> gzPersonnelFoundations = new ArrayList<>();
        List<GzPersonnelFoundation> list=iGzPersonnelFoundationService.lambdaQuery().list();
        for (GzPersonnelFoundation gzPersonnelFoundation : list) {
            GzMonthlyLeave gzMonthlyLeave=new  GzMonthlyLeave();
            gzMonthlyLeave.setMonth(gzGenerateReq.getYearMonth());
            gzMonthlyLeave.setUserId(gzPersonnelFoundation.getId());
            gzMonthlyLeave.setUserName(gzPersonnelFoundation.getName());
            gzMonthlyLeave.setGearopening(gzPersonnelFoundation.getDepartmentName());
            gzMonthlyLeave.setMonthlyDays((double) daysInMonth);

            GzPersonnelFoundation personnelFoundationOne = Convert.convert(new TypeReference<GzPersonnelFoundation>() {}, gzPersonnelFoundation);
            if (gzPersonnelFoundation.getMonthlyDays()==null){
                gzMonthlyLeave.setRequireDateNdancedays((double) (daysInMonth));
                personnelFoundationOne.setRequireDateNdancedays((double) (daysInMonth));
            }else {
                gzMonthlyLeave.setRequireDateNdancedays( (daysInMonth-gzPersonnelFoundation.getMonthlyDays()));
                personnelFoundationOne.setRequireDateNdancedays((daysInMonth-gzPersonnelFoundation.getMonthlyDays()));
            }
            gzMonthlyLeave.setRestDays(gzPersonnelFoundation.getMonthlyDays());
            gzPersonnelFoundations.add(personnelFoundationOne);
            gzMonthlyLeaves.add(gzMonthlyLeave);
        }
        iGzPersonnelFoundationService.updateBatchById(gzPersonnelFoundations);
        return AjaxResult.success(iGzMonthlyLeaveService.saveBatch(gzMonthlyLeaves)?1:0);
    }

    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-18
     * @Param gzMonthlyLeave
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:monthlyleave:export')" )
    @Log(title = "导出月度休假" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzMonthlyLeaveReq gzMonthlyLeaveReq) {
        QueryWrapper<GzMonthlyLeave> lqw = new QueryWrapper<GzMonthlyLeave>();
        gzMonthlyLeaveReq.generatorQuery(lqw,true);
        List<GzMonthlyLeaveVo> list = iGzMonthlyLeaveService.listVo(lqw);
        ExcelUtil<GzMonthlyLeaveVo> util = new ExcelUtil<GzMonthlyLeaveVo>(GzMonthlyLeaveVo. class);
        return util.exportExcel(list, "monthlyleave" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-18
     * @Param gzMonthlyLeave
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:monthlyleave:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzMonthlyLeaveVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzMonthlyLeaveService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-18
     * @Param gzMonthlyLeave
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:monthlyleave:remove')" )
    @Log(title = "删除月度休假" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzMonthlyLeaveService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
